﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public class MonHocProvider
    {
        public static List<MonHoc> LayDanhSachMonHoc()
        {
            var lst = new List<MonHoc>();
            using (var context = new QLTTTHEntities())
            {
                var query = (from u in context.MonHocs select u);

                foreach (var row in query)
                {
                    var mh = new MonHoc();
                    mh.MaMH = row.MaMH;
                    mh.TenMH = row.TenMH;
                    mh.ThoiGianHoc = row.ThoiGianHoc;

                    lst.Add(mh);
                }
            }

            return lst;
        }

        public static bool XoaMonHoc(int maMH)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    //var mh = context.MonHocs.Single(m => m.MaMH.Equals(maMH));

                    //context.MonHocs.Remove(mh);
                    //context.SaveChanges();

                    var query = (from u in context.MonHocs
                                 where u.MaMH.Equals(maMH)
                                 select u).First();

                    context.MonHocs.Remove(query);
                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool CapNhatMonHoc(MonHoc mh)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    var query = (from u in context.MonHocs
                                 where u.MaMH.Equals(mh.MaMH)
                                 select u).First();
                    query.TenMH = mh.TenMH;
                    query.ThoiGianHoc = mh.ThoiGianHoc;

                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }


        public static int ThemMonHoc(MonHoc mh)
        {
            int maMH = -1;
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    context.MonHocs.Add(mh);
                    context.SaveChanges();
                    maMH = mh.MaMH;
                }
            }
            catch
            {
                
            }
            return maMH;
        }

    }
}
